package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.dao.TransactionDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashReceivable;
import sanrenxing.dayEnd.backRepertoryTotal.service.MoneyitiesService;
import sanrenxing.dayEnd.backRepertoryTotal.service.TransactionService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desrciption 现金应收应付 控制器类
 * @auth：yidu 梁树仁
 * @date:2021/3/13 0013 - 16:47
 * @version:1:0
 */
@RestController
@RequestMapping("/moneycontrollers")
public class MoneyitiesController {
    //自动装配
    @Autowired
    private MoneyitiesService moneyitiesService;
    //调用证券交易数据中的接口
    @Autowired
    private TransactionService transactionService;
    //调用现金账户
    @Autowired
    private TransactionDao transactionDao;
    /**
     * 分页查询
     * @param req 请求对象
     * @param page 页数
     * @param limit 行数
     * @return 返回map集合从后端填充数据到前端
     */
    @GetMapping("/moneyities")
    public Map<String,Object> selectEquity(HttpServletRequest req, int page, int limit){
        //构造一个新对象用于保存
        CashReceivable moneyities= new CashReceivable();
        //接收来自请求中的权益除权日
        String businDate = req.getParameter("businDate");
        //判断不为空切不等于零
        if(businDate!=null && businDate.trim().length()!=0){
            //创建一个日期String转换为日期格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //保存权益数据
            try {
                moneyities.setBusinDate(sdf.parse(businDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //调用数据方法根据权证对象构造多条件数据
        String getcondition = moneyitiesService.getcondition(moneyities);
        //执行多条件查询分页
        List<CashReceivable> dataList = moneyitiesService.findByPage(limit, page, getcondition);
        //多表查询表中总记录数
        int count = moneyitiesService.count(getcondition);
        //定义映射集合对象
        HashMap<String, Object> marketMap = new HashMap<>();
        //在映射集合中添加key为code，值为0的状态码
        marketMap.put("code",0);
        //在映射集合中添加key为msg,值为响应信息字符串
        marketMap.put("msg","");
        //在映射集合中添加key为data,值为员集合对象
        marketMap.put("data",dataList);
        //在映射集合中添加key为count,值总行数的数据对
        marketMap.put("count",count);
        //基金
        marketMap.put("fund",transactionService.findFund());
        //现金账户
        marketMap.put("cashAccount",transactionDao.queryCash());
        return marketMap;
    }
    /**
     * 新增方法
     * @param moneyities 对象
     * @return 0,failure 1，SUCCESS
     */
    @PostMapping("/moneyities")
    public String add(@RequestBody CashReceivable moneyities){
        //获取添加的行数
        int rows = moneyitiesService.add(moneyities);
        //使用三元运算符判断
        return rows>0?"add SUCCESS":"add failure";
    }
    /**
     * 删除方法
     * @param moneyId 证券编号
     * @return 0,failure 1，SUCCESS
     */
    @DeleteMapping("/moneyities/{moneyId}")
    //PathVariable：接收请求路径中占位符的值括号中可有可无但是必须一样
    public String add(@PathVariable("moneyId") int moneyId){
        //获取删除的行数
        int rwos=moneyitiesService.delete(moneyId);
        //使用三元运算符判断
        return rwos>0?"delete SUCCESS":"delete failure";
    }
    /**
     * 修改方法方法
     * @param moneyities 对象
     * @return 0,failure 1，SUCCESS
     */
    @PutMapping("/moneyities")
    //RequestBody:表示前端传递的json格式转化为javaBean即domain
    //ResponseBody:表示后端传递的javaBean格式转化为json格式
    public String update(@RequestBody CashReceivable moneyities){
        //获取修改的行数
        int rwos=moneyitiesService.update(moneyities);
        //使用三元运算符判断
        return rwos>0?"update SUCCESS":"update failure";
    }

    /**
     * 批量删除
     * @param moneyIdStr 数据证券ID
     * @return 0,failure 1，SUCCESS
     */
    @PutMapping("/batchBel/{moneyIdStr}")
    public String batchBeleteById(@PathVariable("moneyIdStr") String moneyIdStr) {
        //对员工编号字符串按逗号分隔
        String[] empNos = moneyIdStr.split(",");
        //定义返回值
        String result = "delete success";
        //循环遍历出证券数据id
        for (String equityData : empNos) {
            //调用业务层删除
            int rows = moneyitiesService.delete(Integer.parseInt(equityData));
            //判断
            if (rows <= 0) {
                return "batch delete failure";
            }
        }
        return result;
    }

    /**
     * 现金应收应付对冲销
     * @return
     */
    @DeleteMapping("/method")
    public String washDelete(){
        //调用对冲销方法
        return moneyitiesService.washDelete()?"success":"fail";
    }
}
